function [ Y2_hat, Y2_out ] = applyMLKNN( X1, Y1, X2, Y2, Num, Prior, PriorN, Cond, CondN )

train_data = X1;
train_target = Y1';
test_data = X2;
test_target = Y2';

[~,num_training]=size(train_target);
[num_class,num_testing]=size(test_target);

%Computing distances between training instances and testing instances
dist_matrix=zeros(num_testing,num_training);
for i=1:num_testing
    vector1=test_data(i,:);
    for j=1:num_training
        vector2=train_data(j,:);
        dist_matrix(i,j)=sqrt(sum((vector1-vector2).^2));
    end
end

%Computing Outputs
Outputs=zeros(num_class,num_testing);
for i=1:num_testing
    [~,Neighbors_i] = mink(dist_matrix(i,:), Num);
    neighbor_labels = train_target(:, Neighbors_i);
    
    temp = sum(neighbor_labels' == 1);
    for j=1:num_class
        Prob_in=Prior(j)*Cond(j,temp(1,j)+1);
        Prob_out=PriorN(j)*CondN(j,temp(1,j)+1);
        if(Prob_in+Prob_out==0)
            Outputs(j,i)=Prior(j);
        else
            Outputs(j,i)=Prob_in/(Prob_in+Prob_out);
        end
    end
end

Y2_out = Outputs';
Y2_hat = ones(size(Y2_out));
Y2_hat(Y2_out < 0.5) = -1;

end
